package bitetorrent.model;

import bitetorrent.Torrent;
import bitetorrent.helper.FileManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author user
 */
public class FileDownloader extends Pieces {
    public Torrent torrent;
    public List<PeerPieces> peers = new ArrayList<PeerPieces>();

    public FileDownloader(Torrent torrent) {
        super(torrent.getNumberPiece());
        this.torrent = torrent;
        
    }

    public byte[] getPiece(int index, int begin, int length) {
        return FileManager.readBlockData(torrent.fileName, index * torrent.pieceSize + begin, length);
    }

    public int nextPieceToDownload(String peerAddress) {
        int piece = -1;

        for (int i = 0; i < container.length; ++i) {
        }

        return piece;
    }

    public void addBitField(String address, byte[] bitField) {
        int length = bitField.length;
        PeerPieces peerPieces = new PeerPieces(length, address);
        
        
    }

    public PeerPieces findPeer(String peerAddress) {
        PeerPieces peer = null;

        for (int i = 0; i < peers.size(); ++i) {
            if (peers.get(i).peerAddress.equals(peerAddress)) {
                return peers.get(i);
            }
        }

        return peer;
    }
}
